/**
 * 
 */
package boundary.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import executor.QuadTree;

import boundary.ChildWindow;
import boundary.MainWindow;

/**
 * @author davide
 *
 */
public class CompressWindow extends JToolBar
{
    private static final long serialVersionUID = 6334949258119544959L;
    
    private Point origin;
    private int zoomFactor;
    private QuadTree qTree;
    private ImageIcon image;
    private Color internalColor, externalColor;
    
    private JRadioButton rbt;
    private JColorChooser tcc;
    private ChildWindow parent;
    private boolean isInternalChooser;
    public boolean isThisImageAlreadyCompressed = false;
    
    
    public CompressWindow(String title, ChildWindow ancestor)
    {
	super(title);
	this.setFloatable(true);
	this.setOrientation(VERTICAL);
	this.setLayout(new BorderLayout(0, 0));

	this.parent = ancestor;
	this.cacheParameters();
	
	this.add(this.createToolBarContent(), BorderLayout.PAGE_START);
	this.setVisible(false);
    }

    protected JPanel createToolBarContent()
    {
	JPanel panel = new JPanel(new BorderLayout());
	
	panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
	
	panel.add(this.createToolBarRadioButtons(), BorderLayout.NORTH);
	panel.add(this.createToolBarColorChooser(), BorderLayout.CENTER);
	panel.add(this.createToolBarConfirmButtons(), BorderLayout.SOUTH);
	
	return panel;
    }
    
    protected JPanel createToolBarRadioButtons()
    {
	JRadioButton button;
	JPanel panel = new JPanel();
	ButtonGroup group = new ButtonGroup();
	
	panel.setBorder(BorderFactory.createTitledBorder("Tolleranza"));
	
	// External
	this.rbt = new JRadioButton("Generale");
	this.rbt.setSelected(true);
	
	this.rbt.addActionListener(new ActionListener() {
	    @SuppressWarnings("synthetic-access")
            @Override
	    public void actionPerformed(@SuppressWarnings("unused") ActionEvent e)
	    {
		CompressWindow.this.isInternalChooser = false;
		CompressWindow.this.setChooserColor();
	    }
	});
	
	panel.add(this.rbt);
	group.add(this.rbt);
	
	// Internal
	button = new JRadioButton("Particolare");
	button.setSelected(false);
	
	button.addActionListener(new ActionListener() {
	    @SuppressWarnings("synthetic-access")
            @Override
	    public void actionPerformed(@SuppressWarnings("unused") ActionEvent e)
	    {
		CompressWindow.this.isInternalChooser = true;
		CompressWindow.this.setChooserColor();
	    }
	});
	
	panel.add(button);
	group.add(button);
	
	// That's all folks!
	return panel;
    }
    
    protected JPanel createToolBarColorChooser()
    {
	JPanel panel = new JPanel();
	
	panel.setBorder(BorderFactory.createTitledBorder("Colore"));
	
	this.tcc = new JColorChooser(this.internalColor);
	
	this.tcc.getSelectionModel().addChangeListener(new ChangeListener() {	    
	    @SuppressWarnings("synthetic-access")
            @Override
	    public void stateChanged(@SuppressWarnings("unused") ChangeEvent e)
	    {
		if(CompressWindow.this.isInternalChooser == true) {
		    CompressWindow.this.parent.getContainer().setInternalColor(
			CompressWindow.this.tcc.getColor());
		}
		
		else {
		    CompressWindow.this.parent.getContainer().setExternalColor(
				CompressWindow.this.tcc.getColor());
		}
		
		// update image accordly
		CompressWindow.this.updateImage();
	    }
	});
	
	panel.add(this.tcc.getChooserPanels()[2]);
	return panel;
    }
    
    protected JPanel createToolBarConfirmButtons()
    {
	JButton button;
	JPanel panel = new JPanel();
	
	panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
	
	// Confirm button
	button = new JButton("Conferma");
	button.setToolTipText("Rendi permanenti le selezioni di colore attuale");
	
	button.addActionListener(new ActionListener() {
	    @SuppressWarnings("synthetic-access")
            @Override
	    public void actionPerformed(@SuppressWarnings("unused") ActionEvent e)
	    {
		MyJLabel container = CompressWindow.this.parent.getContainer();
		
		if(!container.getInternalColor().equals(CompressWindow.this.internalColor) ||
			!container.getExternalColor().equals(CompressWindow.this.externalColor))
		{
		    CompressWindow.this.parent.setNeedsToBeSaved(true);
		    
		    // refresh title
		    CompressWindow.this.parent.setTitle(
			    CompressWindow.this.parent.getTitle());
		}
		
		CompressWindow.this.setVisible(false);
		CompressWindow.this.parent.pack();
		
		CompressWindow.this.isThisImageAlreadyCompressed = true;
		MainWindow.menuShortcutsBugWorkaround();
	    }
	});
	
	panel.add(button);
	
	// Cancel button
	button = new JButton("Annulla");
	button.setToolTipText("Annulla le selezioni di colore attuale");
	
	button.addActionListener(new ActionListener() {
	    @SuppressWarnings("synthetic-access")
            @Override
	    public void actionPerformed(@SuppressWarnings("unused") ActionEvent e)
	    {
		CompressWindow.this.setVisible(false);
		CompressWindow.this.rbt.setSelected(true);
		CompressWindow.this.isInternalChooser = false;
		
		CompressWindow.this.restoreParameters();
		CompressWindow.this.parent.pack();
		MainWindow.menuShortcutsBugWorkaround();
	    }
	});
	
	panel.add(button);
	
	// That's all folks!
	return panel;
    }
    
    public void updateImage()
    {
	MyJLabel container = this.parent.getContainer();
	
	if(container.getPolygon().isClosed()) {
	    container.getQuadTree().prepareImage(
		    container.getExternalColor(),
		    container.getPolygon(),
		    container.getInternalColor());
	}
	
	else {
	    container.getQuadTree().prepareImage(container.getExternalColor());
	}
	
	container.setContent(new ImageIcon(container.getQuadTree().getImage()));
	container.scaleImageContent(container.getZoomFactor());
    }
    
    protected void initializeImage()
    {
	MyJLabel container = this.parent.getContainer();
	
	if(container.getPolygon().isClosed()) {
	    container.getQuadTree().prepareImage(
		    container.getExternalColor(),
		    container.getPolygon(),
		    container.getInternalColor());
	}
	
	else {
	    container.getQuadTree().prepareImage(container.getExternalColor());
	}
	
	container.traslateAllGraphics(-container.activeImage.x, -container.activeImage.y);
	
	container.setContent(new ImageIcon(container.getQuadTree().getImage()));
	container.scaleImageContent(-1);
    }

    /* (non-Javadoc)
     * @see javax.swing.JComponent#setVisible(boolean)
     */
    @Override
    public void setVisible(boolean aFlag)
    {
	if(aFlag == true) {
	    this.cacheParameters();
	    this.setChooserColor();
	    
	    this.initializeImage();
	}
	
	super.setVisible(aFlag);
	this.parent.pack();
    }
    
    private void cacheParameters()
    {
	MyJLabel container = this.parent.getContainer();
	
	this.image = container.getContent();
	this.qTree = container.getQuadTree();
	this.internalColor = container.getInternalColor();
	this.externalColor = container.getExternalColor();
	this.zoomFactor = container.getZoomFactor();
	
	this.origin = new Point(container.getActiveImage().x,
		container.getActiveImage().y);
    }
    
    private void restoreParameters()
    {
	MyJLabel container = this.parent.getContainer();
	
	container.traslateAllGraphics(this.origin.x, this.origin.y);
	container.setContent(this.image);
	
	container.setZoomFactor(this.zoomFactor);
	container.scaleImageContent(this.zoomFactor);
	
	container.setQuadTree(this.qTree);
	container.setInternalColor(this.internalColor);
	container.setExternalColor(this.externalColor);
    }
    
    private void setChooserColor()
    {
	if(this.isInternalChooser == true) {
	    this.tcc.setColor(this.parent.getContainer().getInternalColor());
	}
	
	else {
	    this.tcc.setColor(this.parent.getContainer().getExternalColor());
	}
    }
}
